package com.cqc.jdk8.Jutil.stream;

import com.cqc.eb.UserEB;
import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * sorted() 自然排序
 * sorted(Comparator com)自定义排序
 */
public class SortTest {


    List<UserEB> userEBList = null;
    List<UserEB> userEBList2 = null;
    List<String> dimenIdList = null;

    @Before
    public void before() {
        this.getUserEBList();
        this.getDimenIdList();
        this.getUserEBList2();
    }


    /**
     * 按照年龄排序  升序
     */
    @Test
    public void f1() {
        userEBList.stream()
                .sorted(Comparator.comparingInt(user -> user.getAge()))
                .forEach(System.out::println);
    }

    /**
     * 按照年龄排序  降序
     */
    @Test
    public void f2() {
        userEBList.stream()
                //错误
//                .sorted(Comparator.comparingInt(user->user.getAge()).reversed())
                .sorted(Comparator.comparingInt(UserEB::getAge).reversed())
                .forEach(System.out::println);
    }

    @Test
    public void f3() {
        //默认升序 0001+0005+0010+0014+0015+0017
        String collect = dimenIdList.stream()
                .sorted()
                //.sorted((t1,t2)->t1.compareTo(t2))//String::compareTo
                //.sorted((t1,t2)->t2.compareTo(t1))//Comparator.reverseOrder()
                .collect(Collectors.joining("+"));
        System.out.println("collect=" + collect);
    }

    /**
     * 先按A排序，再按B排序
     */
    @Test
    public void f4() {
        //先按 薪资 升序排列，再按年龄升序排列
        userEBList2.stream()
                .sorted(Comparator.comparing(UserEB::getSalary)
                        .thenComparing(UserEB::getAge))
                .forEach(System.out::println);
        System.out.println("--------------------");
        //先按 薪资 降序排列，再按年龄降序排列。 reversed()对前面的几个条件都有效
        userEBList2.stream()
                .sorted(Comparator.comparingDouble(UserEB::getSalary)
                        .thenComparingInt(UserEB::getAge)
                        .reversed()
                )
                .forEach(System.out::println);
    }

    /**
     *
     */
    @Test
    public void f5() {
        //先按 薪资 降序排列，再按年龄升序排列
        userEBList2.stream()
                .sorted(Comparator.comparing(UserEB::getSalary, Comparator.reverseOrder())
                        .thenComparing(UserEB::getAge))
                .forEach(System.out::println);
        System.out.println("--------------------");
        userEBList2.stream()
                .sorted(Comparator.comparing(UserEB::getSalary, Comparator.reverseOrder())
                        .thenComparing(UserEB::getAge, Comparator.reverseOrder()))
                .forEach(System.out::println);
    }

    private void getUserEBList() {
        userEBList = new ArrayList<>();
        userEBList.add(new UserEB("刘诗诗", 33));
        userEBList.add(new UserEB("刘亦菲", 33));
        userEBList.add(new UserEB("古力娜扎", 30));
        userEBList.add(new UserEB("陈都灵", 29));
        userEBList.add(new UserEB("杨紫", 28));
        userEBList.add(new UserEB("林青霞", 60));
    }

    private void getUserEBList2() {
        userEBList2 = new ArrayList<>();
        userEBList2.add(new UserEB("刘诗诗", 33, 2000));
        userEBList2.add(new UserEB("刘亦菲", 33, 2500));
        userEBList2.add(new UserEB("古力娜扎", 30, 1500));
        userEBList2.add(new UserEB("陈都灵", 29, 1500));
        userEBList2.add(new UserEB("杨紫", 28, 1500));
        userEBList2.add(new UserEB("林青霞", 60, 3000));
    }


    private void getDimenIdList() {
        dimenIdList = new ArrayList<>();
        dimenIdList.add("0015");
        dimenIdList.add("0014");
        dimenIdList.add("0010");
        dimenIdList.add("0017");
        dimenIdList.add("0005");
        dimenIdList.add("0001");
    }

}
